//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    
    swiperCurrent: 0,
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 800,
    circular: true,
    imgUrls: [],
    nextpageUrl:'../order/order?name=liu&id=1',
    links: [
      '../order/order',
      '../order/order',
      '../order/order'
    ],
    children1:[
      {
        child_id: 1,
        name: '丰泽概览',
        image: "../../images/indexIMG/icon_fzgl@3x.png",
        nextpageUrl: '../gailan/gailan'
      },
      {
        child_id: 2,
        name: '行程计划',
        image: "../../images/indexIMG/icon_xcjh@3x.png",
        nextpageUrl: '../travelplan/travelplan'
      },
      {
        child_id: 3,
        name: '周边搜索',
        image: "../../images/indexIMG/icon_zbss@3x.png",
        nextpageUrl: '../nearsearch/nearsearch'
      }
    ],
    children2:[
      {
        child_id: 1,
        name: '食在丰泽',
        image: "../../images/indexIMG/icon_szfz@3x.png",
        nextpageUrl: '../meishi/meishi'
      },
      {
        child_id: 2,
        name: '行在丰泽',
        image: "../../images/indexIMG/icon_xzfz@3x.png",
        nextpageUrl: '../travel/travel'
      },
      {
        child_id: 3,
        name: '购在丰泽',
        image: "../../images/indexIMG/icon_gzfz@3x.png",
        nextpageUrl: '../shop/shop'
      },
      {
        child_id: 4,
        name: '住在丰泽',
        image: "../../images/indexIMG/icon_zzfz@3x.png",
        nextpageUrl: '../zhusu/zhusu'
      },
      {
        child_id: 5,
        name: '游在丰泽',
        image: "../../images/indexIMG/icon_yzfz@3x.png",
        nextpageUrl: '../scene/scene'
      },
      {
        child_id: 6,
        name: '娱在丰泽',
        image: "../../images/indexIMG/icon_yuzfz@3x.png",
        nextpageUrl: '../enjoy/enjoy'
      }
    ],
    children3:[
      {
        child_id: 1,
        name: '新闻热点',
        image: "../../images/indexIMG/icon_xwrd@3x.png",
        nextpageUrl: '../hotnews/hotnews'
      },
      {
        child_id: 2,
        name: '旅游攻略',
        image: "../../images/indexIMG/icon_lygl@3x.png",
        nextpageUrl: '../tourismgonglue/tourismgonglue'  
      },
      {
        child_id: 3,
        name: '丰泽礼物',
        image: "../../images/indexIMG/icon_fzlw@3x.png",
        nextpageUrl: '../gift/gift'
      }
    ]
  },
  //获取广告列表
  getADList: function () {
    var that = this;
    app.apiPost(app.apiList.adList, {
      content:{
        moduleId:1
      }
    }, function (res) {
      console.log(res, '广告');

      if (res.content.length < 1) return;
      that.setData({
        imgUrls: res.content, 
      })
    })

  },
  //轮播图的切换事件
  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  //点击指示点切换
  chuangEvent: function (e) {
    this.setData({
      swiperCurrent: e.currentTarget.id
    })
  },
  //点击图片触发事件
  swipclick: function (e) {
    console.log(this.data.swiperCurrent);
    wx.switchTab({
      url: this.data.links[this.data.swiperCurrent]
    })
  },


  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    //收集、判断系统信息
    app.util.checkSystemInfo();


    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    
    // 获取新的token
    
    // if (this.data.hasUserInfo && this.data.canIUse){
    //   this.login();
    // }
    //获取广告列表
    this.getADList();
  },
  // 每次打开页面都会调用一次。
  onShow: function () {
    
  },
  // 发送数据到自己后台
  login:function(){
    app.apiPost(app.apiList.do_login, {
      content:{
        wxCode: app.globalData.wx_code,
        nickname: app.globalData.userInfo.nickName,
        avatarurl: app.globalData.userInfo.avatarUrl,
        appId: 'wx3434b64fab872bfb'
      }
    }, function (res) {
      console.log('发送数据到自己后台', res, res.token);
      wx.setStorageSync('token', res.token);

    })
  },
  /**
   * 获取用户信息
   */
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })

    // app.util.getOpenid();
    
    // console.log(app.globalData.openid, app.globalData.userInfo.nickName, app.globalData.userInfo.avatarUrl,'++++++++++');
    
    this.login();
    
  }
})
